package com.bwhx.eruler.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bwhx.eruler.bean.Notice;
import com.bwhx.eruler.bean.Page;
import com.bwhx.eruler.bean.Staff;
import com.bwhx.eruler.bean.User;
import com.bwhx.eruler.bean.Version;
import com.bwhx.eruler.service.NoticeService;
import com.bwhx.eruler.service.UserService;
import com.bwhx.eruler.service.VersionService;
import com.bwhx.eruler.util.MD5Utils;
import com.bwhx.eruler.util.StringUtils;

/**
  管理通知
 * @author Cuilitang
 *
 */
@Controller
public class NoticeController extends BaseController {
	
	@Autowired
	NoticeService noticeService;
	@ResponseBody
	@RequestMapping(value = "/page.everyone.noticeList.ashx")
	 public Map staffList(Integer page,Integer rows,Notice notice, @RequestBody String b) {
		 log.info(b);
		//每页的开始记录
        int start = (page-1)*rows;
        Page p = new Page(start,rows,notice);
		Map<String,Object> map = new HashMap<String,Object>();
		try{
			List<Notice> list = noticeService.list(p);
			Integer i  = noticeService.count(notice);


			map.put("total",i );//total键 存放总记录数，必须的  A
			map.put("rows",list);//rows键 存放每页记录 list 
			
		}catch(Exception e){
			log.error(getTrace(e));
			map.put("meg", "失败！");
			map.put("status", "0");
		}
		
	    return map;  
	  }
	
	@ResponseBody
	@RequestMapping(value = "/page.saveOrUpdateNotice.ashx")
	public String saveOrUpdateStaff(Notice notice) {
		try{
			if(notice.getnId()==-1){
				log.info("增加通知成功");
				log.info(notice);
				notice.setnId(null);
				notice.setnSendDateTime(System.currentTimeMillis()/1000);
				noticeService.insert(notice);
			}else{
				log.info("修改通知成功");
				log.info(notice);
				noticeService.update(notice);

			}
			
		}catch(Exception e){
			log.error(getTrace(e));
			return "fail";
		}
		
		
	    return "success";  
	  }
	
	
	@ResponseBody
@RequestMapping(value = "/page.noticedel.ashx")
 public Map<String,String> OfflineOrderDel(@RequestParam String nId){
	Map<String,String> resultMap = new HashMap<String, String>();
	resultMap.put("status", "1");
	noticeService.del(nId);

    return resultMap;  
  }
	
}
